package com.example.designpatterns.behavioral.strategy.factory;

import com.example.designpatterns.behavioral.strategy.commons.Payment;
import com.example.designpatterns.behavioral.strategy.core.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 支付策略管理类
 * 是一个简单工厂，用于提前装在策略组，避免新增策略时影响旧代码
 *
 * @author scott
 */
public class PayStrategy {

    /**
     * 默认策略
     */
    public static final Payment DEFAULT_PAY = Payment.ALI_PAY;

    private static final Map<Payment, AbstractPayment> STRATEGY = new HashMap<>();

    /*
     * 初始化策略组
     */
    static {
        STRATEGY.put(Payment.ALI_PAY, new AliPay());
        STRATEGY.put(Payment.JD_PAY, new JdPay());
        STRATEGY.put(Payment.WECHAT_PAY, new WechatPay());
        STRATEGY.put(Payment.UNION_PAY, new UnionPay());
    }

    /**
     * 根据策略key获取具体业务实现
     *
     * @param payKey 策略key
     * @return 具体实现
     */
    public static AbstractPayment get(Payment payKey) {
        if (!STRATEGY.containsKey(payKey)) {
            return STRATEGY.get(DEFAULT_PAY);
        }
        return STRATEGY.get(payKey);
    }

}
